//登录框指令

    //页头指令
app.directive("pageHeader", function() {
        return {
            restrict: "EA",
            templateUrl: "/views/directives/page-header.html",
            replace: true,
            scope: [],
            link: function(element, scope, attr) {}
        }
    })
    //左侧栏指令 
app.directive("leftAside", function() {
        return {
            restrict: "EA",
            templateUrl: "/views/directives/left-aside.html",
            replace: true,
            scope: {
                data: "="
            },
            link: function(scope, element, attr) {
                console.log(scope.data);
                var el = document.querySelectorAll(".left-aside .mui-table-view .mui-collapse-content p");
                angular.element(el).bind("click", function() {
                    var elActive = document.querySelectorAll(".left-aside .mui-table-view .mui-collapse-content .active");
                    angular.element(elActive).removeClass("active");
                    angular.element(this).addClass("active")
                })
            }
        }
    })
    //面包屑指令
app.directive("breadcrumbDirective", function() {
    return {
        restrict: "EA",
        templateUrl: "/views/directives/breadcrumbDirective.html",
        replace: true,
        // scope:[],
        link: function(scope, element, attr) {
          
        }
    }
})
app.directive("editorDirective", function() {
    return {
        restrict: "EA",
        templateUrl: "/views/directives/editorDirective.html",
        replace: true,
        scope: {
            data: "="
        },
        link: function(scope, element, attr) {
            wangEditor.config.printLog = false;
            scope.editor = new wangEditor('editor');
            scope.editor.config.uploadImgUrl = 'http://192.168.99.143:3366/UploadFile/uploadFile';
            scope.editor.config.hideLinkImg = true;
            scope.editor.config.uploadImgFileName = "img";
            scope.editor.config.pasteFilter = false;
            // 自定义load事件
            scope.editor.config.uploadImgFns.onload = function(resultText, xhr) {
                scope.editor.command(null, 'insertHtml', '<img src="http://192.168.99.143:3366/Download/' + resultText + '"/>');
            };
            scope.editor.create();
            scope.$watch('data', function(data) {
                if (data) {
                    scope.editor.$txt.html(data);
                }else{
                    scope.editor.$txt.html("");
                }
            })
        }
    }
})
app.directive("modalDirective", function($state, $interval,$http) {
    return {
        restrict: "EA",
        templateUrl: "/views/directives/dataEdit.html",
        scope: {
            url: "@",
            type: "@",
            data: "=",
            status:"@"
        },
        link: function(scope, element, attr) {
            console.log(scope.type);
            console.log(scope.data);
            console.log(scope.url);
            // scope.$watch('type',function(newData,oldData){
            //     if(newData!=oldData){

            //     }
            // })
            scope.state=$state.current.name;
            scope.$watch("status",function(newData,oldData){
                console.log(newData);
                scope.modalData=scope.data;
                if(newData!=oldData&&newData=='add'){
                    scope.modalData={};
                }
                
            })
            scope.$watch('modalData', function(data) {
                if (data) {
                    scope.informationSources = data.informationSources;
                    scope.createTime = data.createTime;
                    scope.detail = data.detail;
                    scope.pageViews = data.pageViews;
                    scope.title = data.title;
                    if(scope.state=='subject'||scope.state=='category'||scope.state=='major'){
                        scope.id=data.id;
                        scope.ename=data.ename;
                    }
                }
            })
            if(scope.state=='category'||scope.state=='major'){
                $http({
                    method:"get",
                    url:"http://192.168.99.143:3366/"+"BaseDatas/getSubjectList",
                }).success(function(data){
                    console.log(data);
                    scope.majorData=data.subject;
                    if(scope.status="add"){
                        scope.change=function(i){
                            console.log(i);
                            // console.log(scope.subject)
                            angular.forEach(data.subject,function(data){
                                if(data.ecode==i){
                                    scope.category=data.expertiseType;
                                    console.log(scope.category);
                                }
                            })
                        }
                    }
                })
                
            }
            console.log(scope.state);
            console.log(scope.status);
            
            scope.method="post";
            scope.postForm = function() {
                $("form").ajaxSubmit({
                    type: scope.method,
                    dataType: "json",
                    url: scope.url,
                    success: function(data) {
                        console.log(data);
                        if (data.code == 0) {
                            alert(data.msg);
                            $("#modal").modal("hide")
                            $state.reload();
                        } else {
                            alert(data.msg);
                        }
                    },
                    fail: function(data) {

                    }
                })
            }
            scope.close=function(){
                scope.data=null;
                $("#modal").modal("hide")
            }

        }
    }

})

//分页指令
app.directive("paginationDirective", function() {
    return {
        restrict: "EA",
        templateUrl: "/views/directives/pagination.html",
        // replace: true,
        link: function(scope, element, attr) {

        }
    }

})